package com.sinping.qh.api.app.version.v1.records;

import com.sinping.qh.api.app.base.BaseAppEndPoint;
import com.sinping.qh.api.support.*;
import com.sinping.qh.dto.app.records.HiddendangerByAppDTO;
import com.sinping.qh.dto.app.records.RectifyReviewByAppDTO;
import com.sinping.qh.service.app.records.IHiddenDangerService;
import com.sinping.qh.utils.mapper.JsonMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhangjin
 * @create 2018/6/2
 */
@Api(value = "app隐患",description = "app隐患API")
@RestController
@RequestMapping(value =  Const.APP_URI_V1+"/hidden_danger",name="隐患")
public class HiddenDangerEndPointV1 extends BaseAppEndPoint {

    @Autowired
    IHiddenDangerService iHiddenDangerService;

    /***
     * 根据检查记录id,和隐患记录状态查询
     * @param msg
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "app检查记录下的隐患记录")
    @RequestMapping(value="hiddendanger",name = "app检查记录下的隐患记录", method = RequestMethod.GET)
    public ResDto listbyCheckRecordId(@ApiParam(name="msg",
            value="{\"page_size\":10, \"page_current\":1, \"order_column\":\"gmt_created\", \"order_by\":\"desc\"" +
                    ", \"condition\":{\"checkrecord_id\":\"123\",\"hidden_danger_status\":\"[0123]\"}}")
                                    @RequestParam(required = true) String msg)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        FrontPage<HiddendangerByAppDTO> frontPage = JsonMapper.defaultMapper().fromJson(msg, FrontPage.class);

        //查询时检查查询条件格式
        checkNull(frontPage,ConstEum.PARAM_ERROR);

        //获取数据
        frontPage =iHiddenDangerService.findPage(frontPage);

        checkNull(frontPage,ConstEum.FAIL);

        resDto.setMsg(ConstEum.SUCCESS.getMessage());
        resDto.setDatas(JsonMapper.defaultMapper().toJson(frontPage));
        return resDto;
    }

    @ApiOperation(httpMethod = "POST", value = "添加隐患")
    @RequestMapping(name = "添加隐患", method = RequestMethod.POST)
    public ResDto addHiddenDanger(@ApiParam(name="msg",value="{\"id\":\"\",\"check_id\":\"456\",\"check_unit\":\"123\",\"check_unit_id\":\"testunit\",\n" +
            "\"check_building\":\"estbuild\" ,\"check_building_id\":\"textname\",\n" +
            "\"check_lab\":\"textuser\",\"check_lab_id\":\"textuser\",\"check_room\":\"textid\",\n" +
            "\"check_item_one\":\"textudpateid\",\"check_item_one_id\":\"123\"," +
            "\"check_item_two\":\"textudpateid\",\"check_item_two_id\":\"123\",\"desc\":\"电线安全问题\"" +
            ",\"image_url\":[{\"id\":\"ac88fec3b85545c6b00941a915aca925\",\"img_path\":\"\"}],\"check_item_three\":\"123\",\"check_item_three_id\":\"asdf\",\n" +
            "\"hidden_danger_status\":\"0\",\"create_user_id\":\"123\",\"create_user_name\":\"zhj\"}")
                          @RequestParam(required = true) String msg)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        HiddendangerByAppDTO hiddendangerByAppDTO= JsonMapper.defaultMapper().fromJson(msg, HiddendangerByAppDTO.class);
        //查询时检查查询条件格式
        checkNull(hiddendangerByAppDTO,ConstEum.PARAM_ERROR);

        iHiddenDangerService.save(hiddendangerByAppDTO);

        resDto.setMsg(ConstEum.SUCCESS.getMessage());
        return resDto;
    }

    @ApiOperation(httpMethod = "PUT", value = "修改隐患")
    @RequestMapping(name = "修改隐患", method = RequestMethod.PUT)
    public ResDto updateHiddenDanger(@ApiParam(name="msg",value="{\"id\":\"0bd885019e5647cfb19c5b5b5e92b1af\"," +
            "\"check_item_one\":\"textudpateid\",\"check_item_one_id\":\"123\"," +
            "\"check_item_two\":\"textudpateid\",\"check_item_two_id\":\"123\",\"desc\":\"电线安全问题\"" +
            ",\"image_url\":\n" +
            "[{\"id\":\"ac88fec3b85545c6b00941a915aca925\",\"img_path\":\"\"}],\"check_item_three\":\"123\",\"check_item_three_id\":\"asdf\",\"hidden_danger_status\":\"0\"}\n")
                             @RequestParam(required = true) String msg)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        HiddendangerByAppDTO hiddendangerByAppDTO=JsonMapper.defaultMapper().fromJson(msg, HiddendangerByAppDTO.class);

        //查询时检查查询条件格式
        checkNull(hiddendangerByAppDTO,ConstEum.PARAM_ERROR);

        hiddendangerByAppDTO=iHiddenDangerService.update(hiddendangerByAppDTO);
        //转换返回的数据
        String data = JsonMapper.defaultMapper().toJson(hiddendangerByAppDTO);
        resDto.setDatas(data);
        resDto.setMsg(ConstEum.SUCCESS.getMessage());
        return resDto;
    }
    @ApiOperation(httpMethod = "DELETE", value = "删除隐患")
    @RequestMapping(value = "/{id}",name = "删除隐患", method = RequestMethod.DELETE)
    public ResDto deleteHiddenDanger(@PathVariable String id)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        iHiddenDangerService.delete(id);

        resDto.setMsg(ConstEum.SUCCESS.getMessage());
        return resDto;
    }

    @ApiParam(name = "id")
    @ApiOperation(httpMethod = "GET", value = "隐患明细")
    @RequestMapping(value = "/{id}",name = "隐患明细", method = RequestMethod.GET)
    public ResDto hiddenDangerDetail(@PathVariable String id)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        HiddendangerByAppDTO hiddendangerByAppDTO=  iHiddenDangerService.findOne(id);


        String data=JsonMapper.defaultMapper().toJson(hiddendangerByAppDTO);
        resDto.setDatas(data);
        resDto.setMsg(ConstEum.SUCCESS.getMessage());

        return resDto;
    }

    @ApiOperation(httpMethod = "PUT", value = "隐患整改")
    @RequestMapping(value="rectify",name = "隐患整改", method = RequestMethod.PUT)
    public ResDto hiddenDangerRectify(@ApiParam(name="msg",
            value="{\"hidden_danger_id\":\"5b14fc8c6a58d4226c20a701\"" +
                    ",\"image_url\":[{\"id\":\"ac88fec3b85545c6b00941a915aca925\",\"img_path\":\"\"}],\"desc\":\"testdesc\"" +
                    ",\"submit_user_id\":\"user\",\"submit_user_name\":\"username\"}")
                                 @RequestParam(required = true) String msg)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        RectifyReviewByAppDTO rectifyReviewByAppDTO = JsonMapper.defaultMapper().fromJson(msg, RectifyReviewByAppDTO.class);

        //查询时检查查询条件格式
        checkNull(rectifyReviewByAppDTO,ConstEum.PARAM_ERROR);

        iHiddenDangerService.hiddenDangerRectify(rectifyReviewByAppDTO);

        resDto.setMsg(ConstEum.SUCCESS.getMessage());
        return resDto;
    }
    @ApiOperation(httpMethod = "PUT", value = "隐患复查")
    @RequestMapping(value="review",name = "隐患复查", method = RequestMethod.PUT)
    public ResDto hiddenDangerReView(@ApiParam(name="msg",
            value="{\"hidden_danger_id\":\"5b14fc8c6a58d4226c20a701\",\"hidden_danger_status\":\"1\"" +
            ",\"image_url\":[{\"id\":\"ac88fec3b85545c6b00941a915aca925\",\"img_path\":\"\"}],\"desc\":\"testdesc\"" +
            ",\"submit_user_id\":\"user\",\"submit_user_name\":\"username\"}")
                                      @RequestParam(required = true) String msg)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        RectifyReviewByAppDTO rectifyReviewByAppDTO = JsonMapper.defaultMapper().fromJson(msg, RectifyReviewByAppDTO.class);

        //查询时检查查询条件格式
        checkNull(rectifyReviewByAppDTO,ConstEum.PARAM_ERROR);

        boolean ret=true;
        iHiddenDangerService.hiddenDangerReview(rectifyReviewByAppDTO);
        if(ret)
        {
            resDto.setMsg(ConstEum.SUCCESS.getMessage());
        }
        else{
            resDto.setMsg(ConstEum.FAIL.getMessage());
        }
        return resDto;
    }

    @ApiOperation(httpMethod = "GET", value = "整改复查列表")
    @RequestMapping(value="rectifyreview",name = "整改复查列表", method = RequestMethod.GET)
    public ResDto rectifyRecviewByHDId(@ApiParam(name="msg",
            value="{\"page_size\":10, \"page_current\":1, \"order_column\":\"gmt_created\", \"order_by\":\"desc\"" +
                    ", \"condition\":{\"hidden_danger_id\":\"123\"}}")
                                     @RequestParam(required = true) String msg)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        FrontPage<HiddendangerByAppDTO> frontPage = JsonMapper.defaultMapper().fromJson(msg, FrontPage.class);

        //查询时检查查询条件格式
        checkNull(frontPage,ConstEum.PARAM_ERROR);

        //获取数据
        frontPage =iHiddenDangerService.rectifyRecviewByHDId(frontPage);

        checkNull(frontPage,ConstEum.SUCCESS);

        resDto.setMsg(ConstEum.SUCCESS.getMessage());
        resDto.setDatas(JsonMapper.defaultMapper().toJson(frontPage));
        return resDto;
    }

//    @ApiOperation(httpMethod = "POST", value = "添加隐患离线")
//    @RequestMapping(value="update_offline",name = "添加隐患离线", method = RequestMethod.POST)
//    public ResDto updateHiddenDangerByOffLine(@ApiParam(name="msg",value="{\"id\":\"\",\"check_id\":\"456\",\"check_unit\":\"123\",\"check_unit_id\":\"testunit\",\n" +
//            "\"check_building\":\"estbuild\" ,\"check_building_id\":\"textname\",\n" +
//            "\"check_lab\":\"textuser\",\"check_lab_id\":\"textuser\",\"check_room\":\"textid\",\n" +
//            "\"check_item_one\":\"textudpateid\",\"check_item_one_id\":\"123\"," +
//            "\"check_item_two\":\"textudpateid\",\"check_item_two_id\":\"123\",\"desc\":\"电线安全问题\"" +
//            ",\"image_url\":[{\"id\":\"ac88fec3b85545c6b00941a915aca925\",\"img_path\":\"\"}],\"check_item_three\":\"123\",\"check_item_three_id\":\"asdf\",\n" +
//            "\"hidden_danger_status\":\"0\",\"create_user_id\":\"123\",\"create_user_name\":\"zhj\"}")
//                                     @RequestParam(required = true) String msg)
//    {
//        ResDto resDto = new ResDto(ConstEum.SUCCESS);
//
//        HiddendangerByAppDTO hiddendangerByAppDTO= JsonMapper.defaultMapper().fromJson(msg, HiddendangerByAppDTO.class);
//        //查询时检查查询条件格式
//        checkNull(hiddendangerByAppDTO,ConstEum.PARAM_ERROR);
//
//        iHiddenDangerService.updateHiddenDangerByOffLine(hiddendangerByAppDTO);
//
//        resDto.setMsg(ConstEum.SUCCESS.getMessage());
//        return resDto;
//    }

}
